package main

import "fmt"

//2. 学生信息管理系统  id 、name、age、address  （id不能重复）
//
//1、添加学生信息
//2、修改学生信息
//3、删除学生信息
//4、查看当前所有学生信息
//5、退出
type Student struct {
	id int
	name string
	age int
	address string
}
//*[]*Student指向切片的指针，切片的每个元素都是*Student类型
func (s *Student) addStudent(students *[]*Student) {
	//使用切片存储学生信息
	var sid,sage int
	var sname,saddress string
	fmt.Println("请输入id：")
	fmt.Scan(&sid)
	//判断id是否存在
	for _,student := range *students{
		if student.id == sid{
			fmt.Println("Id已存在！")
			return
		}
	}
	fmt.Println("请输入年龄：")
	fmt.Scan(&sage)
	fmt.Println("请输入名字：")
	fmt.Scan(&sname)
	fmt.Println("请输入地址：")
	fmt.Scan(&saddress)
	stu := &Student{id: sid,age: sage,name: sname,address: saddress}
	*students = append(*students,stu)
}
func (s *Student) modStudent(students []*Student,stuid int) {
	var newage int
	var input,newname,newaddress string
	for _,student := range students{
		if student.id == stuid{
			fmt.Println("请输入你想修改的学生信息(1.修改年龄/2.修改名字/3.修改地址)：")
			fmt.Scan(&input)
			switch input {
			case "1":
				fmt.Println("请输入新的年龄：")
				fmt.Scan(&newage)
				student.age = newage
			case "2":
				fmt.Println("请输入新的名字：")
				fmt.Scan(&newname)
				student.name = newname
			case "3":
				fmt.Println("请输入新的地址：")
				fmt.Scan(&newaddress)
				student.address = newaddress
			default:
				fmt.Println("请输入正确格式")
			}
		}
	}
}
func (s *Student) delStudent(students *[]*Student,stuid int) {
	for i,student := range *students{
		if student.id == stuid{
			*students = append((*students)[:i], (*students)[i+1:]...)
			fmt.Println("删除成功")
			return
		}
	}
}
func (s *Student) showStudent(students []*Student)  {
	for _,student := range students{
		fmt.Print("Id:",student.id," ")
		fmt.Print("Age:",student.age," ")
		fmt.Print("Name:",student.name," ")
		fmt.Print("Address:",student.address," ")
		fmt.Println()
	}
}

func main()  {
	students := make([]*Student,0)
	s := Student{}
	for {
	fmt.Println("请输入你的操作(1.添加学生信息/2.修改学生信息/3.删除学生信息/4.展示学生信息/5.退出):")
	var input string
	fmt.Scan(&input)
	switch input {
		case "1":
			s.addStudent(&students)
		case "2":
			var id int
			fmt.Println("请输入修改的学生id:")
			fmt.Scan(&id)
			s.modStudent(students,id)
		case "3":
			var id int
			fmt.Println("请输入删除的学生id:")
			fmt.Scan(&id)
			s.delStudent(&students,id)
		case "4":
			s.showStudent(students)
		case "5":
			return
	}
	}
}
